package com.hskn.hss.module.worktype.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlworktype.entity.TlWorkType;
import com.hskn.hss.module.tlworktype.mapper.TlWorkTypeMapper;
import com.hskn.hss.module.worktype.entity.WorkType;
import com.hskn.hss.module.worktype.mapper.WorkTypeMapper;
import com.hskn.hss.module.worktype.service.IWorkTypeService;
import com.hskn.hss.utils.GroupingByUtils;
import com.hskn.hss.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-17
 */
@Service
public class WorkTypeServiceImpl extends ServiceImpl<WorkTypeMapper, WorkType> implements IWorkTypeService {

    @Resource
    private WorkTypeMapper workTypeMapper;
    @Resource
    private TlWorkTypeMapper tlWorkTypeMapper;

    @Override
    public List<WorkType> mList(JSONObject params) throws HssException {

        QueryWrapper<WorkType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(WorkType::getCreateTime);
        List<WorkType> workTypes = workTypeMapper.selectList(queryWrapper);
        return workTypes;
    }

    @Override
    public Map<String, TlWorkType> getWorkTypeMap(QueryWrapper<TlWorkType> workTypeQueryWrapper)  {
        List<TlWorkType> workTypeList=tlWorkTypeMapper.selectList(workTypeQueryWrapper);
        Map<String, TlWorkType> workTypeMap= new HashMap<>();
        if (!workTypeList.isEmpty()) {
            workTypeMap = GroupingByUtils.getGroupingForMapEntity(workTypeList, TlWorkType::getId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        return workTypeMap;
    }

    @Override
    public void mUpdate(JSONObject params) throws HssException {

        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("workTypeId"))) {
            throw new HssException("参数错误");
        }
        WorkType workType = JSON.parseObject(params.toJSONString(), WorkType.class);
        workTypeMapper.updateById(workType);
    }

    @Override
    public void add(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        WorkType workType = JSON.parseObject(params.toJSONString(), WorkType.class);
        workType.setCreateUid(uid);
        workTypeMapper.insert(workType);
    }

    @Override
    public void del(JSONObject params) throws HssException {

        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("WorkTypeId"))) {
            throw new HssException("参数错误");
        }
        String id = params.getString("WorkTypeId");
        workTypeMapper.deleteById(id);
    }

}
